<?php
/**
 * @todo The flash doesn't seem to be working here. When I edit a product, no flash message is shown.
 */
class Admin_ProductController extends ImpSoft_Controller_Action {

	public function init() {
	
		parent::init();
		$this->catTbl = new Category;
		$this->prdTbl = new Product;
	
	}
	/**
	 * Product selection page (for selecting a product to edit)
	 */
	public function listAction() {
	
		$query = $this->getRequest()->getQuery();
		$page = $this->getRequest()->getParam("page");
		$order = (empty($query['order'])) ? null : $query['order'];
		$dir = (empty($query['dir'])) ? null : $query['dir'];
		$this->view->dir = (strtolower($dir) == "asc") ? "desc" : "asc";
		$select = $this->prdTbl->getListForAdmin($order, $dir);
		$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($select));
		// @todo Eventually this should come from db settings
		$paginator->setItemCountPerPage(100);
		$paginator->setCurrentPageNumber($page);
		$this->view->products = $paginator;
	
	}
	/**
	 * Create a new product
	 */
	public function addAction() {
	
		$product = $this->prdTbl->createRow();
		// build filters from table
		$filters = with(new Q_Filter_Set())
			->add($this->prdTbl->getFilters());
		// build rules from table
		$rules = with(new Q_Rule_Set())
			->add($this->prdTbl->getRules());
		// initialize the errors array
		$errors = array();
		// if data has been posted, handle it
		if ($this->isPost()) {
			$post = $this->getPost();
			// filter post data
			$post = $filters->filter($post);
			// validate filtered data
			if ($rules->validate($post)) {
				$product->setFromArray($post);
				// begin database transaction
				$this->db->beginTransaction();
				try {
					$product->save();
					// commit any queries
					$this->db->commit();
					$this->flash("Product was created successfully", "notice");
					$this->goToRoute("admin_product_add");
					return;
				} catch (Zend_Db_Exception $e) {
					// if any data couldn't be saved, roll back everything
					$this->db->rollback();
					$this->flash("Product could not be saved, sorry!", "error");
					$this->goToRoute("admin_product_add");
					return;
				}
			} else {
				$errors = $rules->getErrors();
			}
		}
		$this->view->errors = $errors;
		$this->view->required = $rules->getRequired();
		$this->view->product = $product->toArray();
	
	}
	/**
	 * Edit a product
	 */
	public function editAction() {
	
		$product_id = $this->getRequest()->getParam('product_id');
		$this->view->tab = $this->getRequest()->getParam('tab');
		$prdset = $this->prdTbl->find($product_id);
		if (!$prdset->count()) {
			$this->flash("The product you attempted to edit doesn't exist.");
			$this->goToRoute("admin_product_list");
			return;
		}
		$product = $prdset->current();
		// build filters from table
		$filters = with(new Q_Filter_Set())
			->add($this->prdTbl->getFilters());
		// build rules from table
		$rules = with(new Q_Rule_Set())
			->add($this->prdTbl->getRules());
		$errors = array();
		if ($this->isPost()) {
			$post = $this->getPost();
			// filter post data
			$post = $filters->filter($post);
			// validate filtered data
			if ($rules->validate($post)) {
				$product->setFromArray($post);
				// begin database transaction
				$this->db->beginTransaction();
				try {
					$product->save();
					// commit any queries
					$this->db->commit();
					// @todo Find out if you should be specifying flash's "namespace" param. This should be "notice"
					$this->flash("Product was updated successfully", "notice");
					$this->goToRoute("admin_product_edit", array("product_id" => $product->product_id));
					return;
				} catch (Zend_Db_Exception $e) {
					// if any data couldn't be saved, roll back everything
					$this->db->rollback();
					// @todo Find out if you should be specifying flash's "namespace" param. This should be "error"
					$this->flash("Product could not be saved, sorry!", "error");
					$this->goToRoute("admin_product_edit", array("product_id" => $product->product_id));
					return;
				}
			} else {
				$errors = $rules->getErrors();
			}
		}
		$this->view->errors = $errors;
		$this->view->required = $rules->getRequired();
		$this->view->product = $product->toArray();
	
	}
	/**
	 * Delete a product
	 */
	public function deleteAction() {
	
		$product_id = $this->getRequest()->getParam('product_id');
		$this->prdTbl->delete($this->prdTbl->getAdapter()->quoteInto('product_id = ?', $product_id));
		$this->flash("Product was deleted successfully");
		$this->goToRoute("admin_product_list");
	
	}

}